# setup the project
# =================

cmake_minimum_required(VERSION 2.6)
project(points2grid)
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/modules" ${CMAKE_MODULE_PATH} )
set(CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE ON)

if (CMAKE_MAJOR_VERSION GREATER 2)
    cmake_policy(SET CMP0042 OLD)
endif()

# set build type
# ==============

if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING
        "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel" FORCE)
endif()
message(STATUS "Setting ${CMAKE_PROJECT_NAME} build type - ${CMAKE_BUILD_TYPE}")

set (P2G_VERSION_MAJOR 1)
set (P2G_VERSION_MINOR 3)
set (P2G_VERSION_PATCH 1)

set (P2G_VERSION_STRING "${P2G_VERSION_MAJOR}.${P2G_VERSION_MINOR}.${P2G_VERSION_PATCH}")

# setup dependencies
# ==================




# GDAL/OGR support - optional, default=ON
option(WITH_GDAL "Choose if GDAL support should be built" FALSE)
if(WITH_GDAL)
    find_package(GDAL 1.9.0)
    if (GDAL_FOUND)
        set(HAVE_GDAL 1)
        include_directories("${GDAL_INCLUDE_DIR}")
        mark_as_advanced(CLEAR GDAL_INCLUDE_DIR)
        mark_as_advanced(CLEAR GDAL_LIBRARY)
        message(STATUS "...building with GDAL")
    else()
        set(WITH_GDAL FALSE)
        set(GDAL_LIBRARY "")
    endif()
endif()

# cURL (optional)
# ---------------

#find_package(CURL)
#include_directories(${CURL_INCLUDE_DIRS})



# bzip2
# -----

#find_package(BZip2)
#if(BZIP2_FOUND)
#    include_directories(${BZIP2_INCLUDE_DIRS})
#else()
#    message(STATUS "BZip2 not found.  Builds using packaged Boost libraries may fail.")
#endif()

# generate our configuration header
# =================================

configure_file(${PROJECT_SOURCE_DIR}/include/points2grid/config.h.in
    ${PROJECT_SOURCE_DIR}/include/points2grid/config.h)

# set flags for GCC (from original Makefile)
# ==========================================

if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX)
    # implicit large file support
    execute_process(COMMAND getconf LFS_CFLAGS
        OUTPUT_VARIABLE LFS_CFLAGS
        ERROR_QUIET)

    add_definitions(
        -Wall
        -g
        -D_GNU_SOURCE
        -D_LARGEFILE_SOURCE
        ${LFS_CFLAGS})
endif()

if(WIN32)
    if (MSVC)
        add_definitions(-D_CRT_SECURE_NO_DEPRECATE)
        add_definitions(-D_CRT_SECURE_NO_WARNINGS)
        add_definitions(-D_CRT_NONSTDC_NO_WARNING)
        add_definitions(-D_SCL_SECURE_NO_WARNINGS)
        add_definitions(-DBOOST_LIB_TOOLSET="vc110")
        add_definitions(-DBOOST_ALL_DYN_LINK)
    endif()
endif()

# and now the actual project that we'll build
# ===========================================

set(SRC_DIR src)
set (INCLUDE_DIR include/points2grid)
set (APPS_DIR apps)

include_directories(
    .
    include)

set(DEFAULT_INCLUDE_SUBDIR include)

set(DEFAULT_BIN_SUBDIR bin)
set(DEFAULT_LIB_SUBDIR lib)

set(LIBRARY_CPP
    ${SRC_DIR}/InCoreInterp.cpp
    )

set(POINTS2GRID_HPP
    ${INCLUDE_DIR}/config.h
    ${INCLUDE_DIR}/CoreInterp.hpp
    ${INCLUDE_DIR}/Global.hpp
    ${INCLUDE_DIR}/GridPoint.hpp
    ${INCLUDE_DIR}/InCoreInterp.hpp
    )

# setup source groups
# -------------------

source_group("CMake Files" FILES CMakeLists.txt)
source_group("Header Files" FILES ${POINTS2GRID_HPP})
source_group("Source Files" FILES ${POINTS2GRID_CPP})


# build the actual executable
# ---------------------------
if (WIN32)
add_compile_definitions(P2G_DLL_EXPORT)
endif()
set(P2G_LIB_NAME pts2grd)
add_library(${P2G_LIB_NAME}
        #SHARED
        ${LIBRARY_CPP} ${POINTS2GRID_HPP})
target_link_libraries(${P2G_LIB_NAME} ${Boost_LIBRARIES} ${CURL_LIBRARIES})
# if (WIN32)
#     set_target_properties(${P2G_LIB_NAME} PROPERTIE
#         COMPILE_DEFINITIONS P2G_DLL_EXPORT)
# endif()

if (BZIP2_FOUND)
    target_link_libraries(${P2G_LIB_NAME} ${BZIP2_LIBRARIES})
endif()

if (GDAL_FOUND)
    target_link_libraries(${P2G_LIB_NAME} ${GDAL_LIBRARY})
endif()

if (APPLE)
    set_target_properties(
        ${P2G_LIB_NAME}
        PROPERTIES
        INSTALL_NAME_DIR "@rpath" BUILD_WITH_INSTALL_RPATH ON)
endif()


#add_executable(points2grid ${APPS_DIR}/points2grid.cpp)
#target_link_libraries(points2grid ${P2G_LIB_NAME}  )
# if (WIN32)
#     set_target_properties(points2grid PROPERTIES
#         COMPILE_DEFINITIONS P2G_DLL_IMPORT)
# endif()


if(ANDROID)
set(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/../../libs/${PROJECT_NAME}/${ANDROID_ABI})
elseif(WIN32)
    if(CMAKE_CL_64)
        if(CMAKE_BUILD_TYPE STREQUAL "Debug")
            set(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/../../libs/${PROJECT_NAME}/Win64d)
        else()
            set(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/../../libs/${PROJECT_NAME}/Win64)
        endif() 
       
    else()
        set(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/../../libs/${PROJECT_NAME}/Win32)
    endif()
elseif(LINUX)
    set(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/../../libs/${PROJECT_NAME}/Linux64)
elseif(IOS)
        string(TOLOWER "${CMAKE_OSX_SYSROOT}" sysroot_lower)
        if(sysroot_lower MATCHES "iphoneos")
                message(STATUS "==> 真机 SDK Detected")
                set(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/../../libs/${PROJECT_NAME}/iOS)
        elseif(sysroot_lower MATCHES "iphonesimulator")
                message(STATUS "==> 模拟器 SDK Detected")
                set(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/../../libs/${PROJECT_NAME}/iOS_simulator)
        endif()
endif()

# install the executable
# ----------------------

#install(TARGETS points2grid
#    RUNTIME DESTINATION ${DEFAULT_BIN_SUBDIR}
#    LIBRARY DESTINATION ${DEFAULT_LIB_SUBDIR}
#    ARCHIVE DESTINATION ${DEFAULT_LIB_SUBDIR})
#
install(TARGETS ${P2G_LIB_NAME}
   RUNTIME DESTINATION ${DEFAULT_BIN_SUBDIR}
   LIBRARY DESTINATION ${DEFAULT_LIB_SUBDIR}
   ARCHIVE DESTINATION ${DEFAULT_LIB_SUBDIR})

install(DIRECTORY ${INCLUDE_DIR}
   DESTINATION ${DEFAULT_INCLUDE_SUBDIR}
   FILES_MATCHING PATTERN "*.h" PATTERN "*.hpp")

# tests
# -----
#option(WITH_TESTS "Choose if tests should be built" TRUE)
#if (WITH_TESTS)
#    enable_testing()
#    set(P2G_TEST_NAME points2grid-test)
#    add_subdirectory(test)
#    add_subdirectory(vendor/gtest-1.7.0)
#endif(WITH_TESTS)
